package com.example.app.adapter.catalog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.app.R;
import com.example.app.viewmodel.catalog.CurrentSubCategory;

import java.util.List;

public class CurrentSubCategoryAdapter extends BaseAdapter {
    private Context ctx;
    private List<CurrentSubCategory> currentSubCategoryList;

    public CurrentSubCategoryAdapter(Context ctx, List<CurrentSubCategory> currentSubCategoryList) {
        this.ctx = ctx;
        this.currentSubCategoryList = currentSubCategoryList;
    }

    @Override
    public int getCount() {
        return this.currentSubCategoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.currentSubCategoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.adapter_catalog_currentsubcategory,null);
            viewHolder.currentSubCategory_img = convertView.findViewById(R.id.iv_currentSubCategory_img);
            viewHolder.currentSubCategory_text = convertView.findViewById(R.id.tv_currentSubCategory_text);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Glide.with(this.ctx)
                .load(this.currentSubCategoryList.get(position).getPicUrl())
                .into(viewHolder.currentSubCategory_img);

        viewHolder.currentSubCategory_text.setText(this.currentSubCategoryList.get(position).getName());

        return convertView;
    }

    static class ViewHolder{
        ImageView currentSubCategory_img;
        TextView currentSubCategory_text;
    }
}
